﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Detail.Cultivation;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model.Cultivation;

namespace Traceability.App.Api.Application.Handler.Detail.Cultivation
{
    public class AddCultivationHandler : IRequestHandler<AddCultivationCommand, ApiResult<int>>
    {
        IBaseRepository<CultivationModel> culRepo;
        IMapper mapper;

        public AddCultivationHandler(IBaseRepository<CultivationModel> culRepo, IMapper mapper)
        {
            this.culRepo = culRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(AddCultivationCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res = new ApiResult<int>();
           
            var model=mapper.Map<CultivationModel>(request);
            int a=culRepo.Add(model).Result;
            if(a>0)
            {
                res.Msg = "添加养殖信息成功";
                res.Code=ApiEnum.Success;
                res.Datas = a;
                return Task.FromResult(res);
            }
            else
            {
                res.Msg = "添加养殖信息失败";
                res.Code = ApiEnum.Fail;
                res.Datas = a;
                return Task.FromResult(res);
            }
        }
    }
}
